package com.bolt.support.spring.jpa.jpql;

import com.bolt.common.utils.StrUtil;

public class ColumnClause<V> extends BaseClause<String,ColumnClause,V> {


    public ColumnClause(String property , Class<V> type) {
        super(property,null,null,type);
        this.owned = createOwnedName(type);
    }

    public ColumnClause(String property ,String alias, Class<V> type) {
        super(property,null,null,type);
        this.alias = alias;
        this.owned = createOwnedName(type);
    }

    public ColumnClause(String property , String owned, String as, Class<V> type) {
        super(property,owned,as,type);
    }


    @Override
    public String render(boolean addAlias ) {
        StringBuffer to = new StringBuffer(60);
        String pattern = templates.get(Ops.PROPERTY);
        to.append(StrUtil.indexedFormat(pattern,this.owned,this.property));

        if(null != this.ops){
           String funcPattern = templates.get(this.ops);
           String temp = to.toString();
            this.args.add(0,temp);
            to.delete(0,temp.length());
            to.append(StrUtil.indexedFormat(funcPattern,this.args.toArray()));
        }

        if(StrUtil.isNotBlank(this.alias)  && addAlias){
             pattern = templates.get(Ops.ALIAS);
             to.append(StrUtil.indexedFormat(pattern,this.alias));
        }
        return to.toString();
    }


}

 